<template>
  <div>
    <div v-show="isVisible" class="moveable-div" :class="{ moved: isMoved }" @click="moveDiv()"
      >图片{{ isMoved }}1</div
    >
    <n-h2>海淀桥测试</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
    <n-h2>海淀桥</n-h2>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';

  let colorobj = reactive({
    x: '500px',
    y: '200px',
  });

  let isVisible = ref<any>(true);
  let isMoved = ref<any>(false); // 创建一个响应式引用 isMoved，并初始化为 false

  const moveDiv = () => {
    isMoved.value = true; // 修改 isMoved 的值，触发动画
    setTimeout(() => {
      // isVisible.value = false; // 动画结束后隐藏 div
    }, 1500); // 等待动画完成后执行
  };
</script>

<style scoped>
  .moveable-div {
    width: 200px;
    height: 200px;
    background-color: #bfa;
    cursor: pointer; /* 鼠标悬停时显示手型图标，表示可点击 */
  }

  .moveable-div.moved {
    animation: move-and-fade 1.5s linear forwards;
  }

  @keyframes move-and-fade {
    0% {
      transform: translate(0, 0); /* 初始位置 */
    }

    100% {
      transform: translate(v-bind('colorobj.x'), v-bind('colorobj.y')); /* 最终位置 */
    }
  }
</style>
